#include "texthtmlmessagerenderer.h"
#include "converse/converseabstractmessage.h"
#include "utils/timeutility.h"

#include <QDateTime>
#include <QDebug>


namespace Text {

HtmlMessageRenderer::HtmlMessageRenderer(QObject *parent) :
    QObject(parent)
{
    m_isTable = false;
}


const QString &HtmlMessageRenderer::htmlDocument()
{
    update();
    return m_htmlDocument;
}


void HtmlMessageRenderer::appendMessage(const Converse::AbstractMessage *message)
{
    m_isTable = true;
    m_messageList.append(message);
}


void HtmlMessageRenderer::insertMessage(int position, const Converse::AbstractMessage *message)
{
    m_isTable = true;
    m_messageList.insert(position, message);
}


void HtmlMessageRenderer::setMessage(const QString &text)
{
    m_isTable = false;
    m_plainMessage = text;
}


void HtmlMessageRenderer::clear()
{
    m_messageList.clear();
    m_htmlDocument.clear();
}


void HtmlMessageRenderer::update()
{
    refresh();
}


QString HtmlMessageRenderer::templateHeader() const
{
    QString htmlHeader = "<head>"
            "<meta name=\"qrichtext\" content=\"0\" />"
            "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />"
            "<title></title>"
            "<style type=\"text/css\"> p, li { white-space: pre-wrap; } </style>"
            "</head>";
    return htmlHeader;
}


QString HtmlMessageRenderer::templateTable() const
{
    QString htmlTable = "<table border=\"0\" align=\"center\" width=\"100%\" cellspacing=\"4\" cellpadding=\"0\">"
            " %1 "
            "</table>";
    return htmlTable;
}


QString HtmlMessageRenderer::templateDocument() const
{
    QString htmlDocument = "<html>";
    htmlDocument.append(templateHeader());
    htmlDocument.append("<body style=\" font-size:14px; font-weight:450; font-style:normal; text-decoration:none;\">");
    htmlDocument.append(" %1 ");
    htmlDocument.append("</body>");
    htmlDocument.append("</html>");
    return htmlDocument;
}


QString HtmlMessageRenderer::templateMessageCell() const
{
    QString htmlMessageCell = \
            "<td>"
            "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">"
            " %1 "
            "</p>"
            "</td>";
    return htmlMessageCell;
}


QString HtmlMessageRenderer::templateTimeCell() const
{
    QString htmlTimeCell = \
            "<td width=\"120\" align=\"right\">"
            "<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">"
            "<span style=\" font-weight:450;\">"
            " %1 "
            "</span>"
            "</p>"
            "</td>";
    return htmlTimeCell;
}


QString HtmlMessageRenderer::htmlMessageRow(const QString &message, const QString &time)
{
    QString htmlRow = "<tr>";
    QString messageCell = templateMessageCell().arg(message);
    htmlRow.append(messageCell);
    QString timeCell = templateTimeCell().arg(time);
    htmlRow.append(timeCell);
    htmlRow.append("</tr>");
    return htmlRow;
}


QString HtmlMessageRenderer::htmlMessageRow(const QString &message, qint64 time)
{
    QDateTime dateTime = QDateTime::fromTime_t(time);
    QString  dateTimeString;
    if (Utility::TimeUtility::isToday(time))
        dateTimeString = dateTime.toString("h:mm");
    else
        dateTimeString = dateTime.toString("h:mm dd-MM-yyyy");

    return htmlMessageRow(message, dateTimeString);
}


void HtmlMessageRenderer::refreshTableMessage()
{
    if (m_messageList.isEmpty()) {
        m_htmlDocument.clear();
        return;
    }

    QString htmlRows = "";
    foreach (const Converse::AbstractMessage *message, m_messageList) {
        Q_ASSERT(message);
        htmlRows.append(htmlMessageRow(message->message(), message->receivedTime()));
    }

    QString htmlTable = templateTable().arg(htmlRows);
    QString htmlDocument = templateDocument().arg(htmlTable);
    m_htmlDocument = htmlDocument;
}


void HtmlMessageRenderer::refreshPlainMessage()
{
    // replace character "<" to display correct html
    QString plainMessage = m_plainMessage.replace("<", "&lt;");
    plainMessage = plainMessage.replace(">", "&gt;");

    // keep code emotion
    plainMessage = plainMessage.replace(":&gt;", ":>");
    QString htmlDocument = templateDocument().arg(plainMessage);
    m_htmlDocument = htmlDocument;
}


void HtmlMessageRenderer::refresh()
{
    if (m_isTable) {
        refreshTableMessage();
        return;
    }

    refreshPlainMessage();
}

} // namespace Text
